package com.kaishengit.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;

import com.kaishengit.dao.GoodsDao;
import com.kaishengit.entity.Goods;
import com.kaishengit.service.GoodsService;
import com.kaishengit.util.PKUtils;

public class AjaxServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		
		String task = request.getParameter("task");
		if("save".equals(task)) {
			save(request,response);
		}else if(task == null || "list".equals(task)){
			findAll(request,response);
		}else if(task.equals("find")){
			findById(request,response);
			
		}else if(task.equals("del")){
			del(request,response);
			
		}else if(task.equals("edit")){
			
			
		}
	}
		


	private void del(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		String id = request.getParameter("id");
		
		GoodsDao dao = new GoodsDao();
		
		dao.del(id);
		
		request.getRequestDispatcher("/WEB-INF/views/indexwithjquery.jsp").forward(request, response);
	}



	private void findById(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String id = request.getParameter("id");
		
		GoodsDao dao = new GoodsDao();
		
		Goods goods = dao.findById(id);
		
		JSONArray json = JSONArray.fromObject(goods);
		
		response.setContentType("application/json;charset=UTF-8");
		
		PrintWriter out = response.getWriter();
		String jsonString = json.toString();
		
		System.out.println(jsonString);
		out.print(jsonString);
		out.flush();
		out.close();
	}


	private void findAll(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		GoodsDao dao = new GoodsDao();
		List<Goods> list = dao.findAll();
		
		HttpSession session = request.getSession();
		session.setAttribute("goodsList", list);
		
		request.getRequestDispatcher("/WEB-INF/views/indexwithjquery.jsp").forward(request, response);
		
	}


	private void save(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String number = request.getParameter("number");
		String name = new String(request.getParameter("name").getBytes("ISO-8859-1"),"UTF-8");
		float price = Float.parseFloat(request.getParameter("price"));
		String yes = request.getParameter("yes");
		String no =request.getParameter("no");
		
		Goods goods = new Goods();
		
		goods.setId(PKUtils.getPk());
		goods.setNumber(number);
		goods.setName(name);
		goods.setPrice(price);
		if("checked".equals(yes)){
			goods.setEnable(true);
		}else if("checked".equals(no)){
			goods.setEnable(false);
		}
		
		GoodsService salesService = new GoodsService();
		salesService.save(goods);
		
		System.out.println(goods.getName());
		
		JSONArray json = JSONArray.fromObject(goods);
		
		response.setContentType("application/json;charset=UTF-8");
		
		PrintWriter out = response.getWriter();
		String jsonString = json.toString();
		
		System.out.println(jsonString);
		out.print(jsonString);
		
		out.flush();
		out.close();
	
	}

}
